package com.proxy.scanandcheck.common.util;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;

/**
 * Datasource util
 *
 */
public class DataSourceUtil {
	
	private static DataSource dataSource = null;
	
	/**
	 * Prepare data source.
	 */
	static{
		if (dataSource == null) {
			try {
				Class.forName(Constants.JDBC_DRIVER);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			BasicDataSource basicDs = new BasicDataSource();
			
			basicDs.setUrl(Constants.URL);
			basicDs.setUsername(Constants.USER);
			basicDs.setPassword(Constants.PASSWORD);
			dataSource =basicDs;
		}
	}
	
	/**
	 * return data source.
	 * @return
	 */
	public static DataSource getDataSource() {
		return dataSource;
	}
	
	/**
	 * Close data source.
	 * @throws SQLException
	 */
	public static void closeDataSource() throws SQLException {
		((BasicDataSource) dataSource).close();
	}

}
